import java.util.Arrays;
import java.util.Scanner;

public class javase08 {
    //    4.1 数组转字符串
    public static String mytoString(int[] array) {
        if (array == null) {
            return null;
        }
        if (array.length == 0) {
            return "";
        }
        String str = "[";
        for (int i = 0; i < array.length; i++) {
            str += array[i];
            if (i != array.length - 1) {
                str += ",";
            }
        }
        return str += "]";
    }

    public static void main23(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6};
        //ret = Arrays.toString(array);
        //System.out.println(ret);
        String ret = mytoString(array);
        System.out.println(ret);
    }

    //    4.2 数组拷贝
    public static int[] copyArray(int[] array) {
        int[] Array = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            Array[i] = array[i];
        }
        return Array;
    }

    public static void main12345(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6};
        int[] ret = copyArray(array);
        System.out.println(Arrays.toString(ret));
    }

    //    4.4 求数组中元素的平均值
    public static double averg(int[] Array) {
        double sum = 0.0;
        for (int i = 0; i < Array.length; i++) {
            sum += Array[i];
        }
        return sum / Array.length;
    }

    public static void main23456(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6, 7,};
        System.out.println(averg(array));
    }

    //    4.5 查找数组中指定元素(顺序查找)
    public static int toFind(int[] arr, int a) {

        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == a) {
                return i;
            }
        }
        return -1;
    }

    public static void main5643(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 89};
        System.out.println(toFind(arr, 89));

    }

    //查找数组中指定元素(二分查找)
    public static int bsearch(int[] arr, int a) {
        int left = 0;
        int right = arr.length - 1;
        while (left <= right) {
            int mid = left + right;
            if (a < arr[mid]) {
                right = mid - 1;
            } else if (a > arr[mid]) {
                left = mid + 1;
            } else {
                return mid;
            }
        }
        return -1;
    }

    public static void main2345678(String[] args) {
        while (true) {
            int[] arr = {1, 2, 3, 4, 5, 6, 7, 8};
            Scanner sc = new Scanner(System.in);
            int x = sc.nextInt();
            System.out.println(bsearch(arr, x));
        }
    }

    //    .7 检查数组的有序性
    public static boolean isSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            if (arr[i] > arr[i + 1]) {
                return false;
            }
        }
        return true;
    }

    public static void main123456(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            int[] arr = {};
            System.out.println(isSort(arr));
        }
    }


    //冒泡排序

    public static void bubblesort(int[] arr) {
        for (int i = 0; i < arr.length-1; i++) {
            for (int j = 0; j < arr.length-1-i; j++) {
                if(arr[j] > arr[j+1]) {
                    int tmp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = tmp;
                }
            }
        }
    }
    public static void main1234598765(String[] args) {
        int[] arr = {9,5,2,7,8,45,23,34,89};
        bubblesort(arr);
        System.out.println(Arrays.toString(arr));
    }


    //数组逆序

    public static void reverse(int[] arr) {

        int left = 0;
        int right = arr.length-1;
        while(left < right) {
            int tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;
            left++;
            right--;
        }
    }

    public static void main8976543(String[] args) {
        int[] arr = {1,2,3,4,5,6,7,8};
        reverse(arr);
        System.out.println(Arrays.toString(arr));
    }

    //数组数字排列,给定一
    // 个整型数组, 将所有
    // 的偶数放在前半部分,
    // 将所有的奇数放在数组后半部分
    public static void trans(int[] arr) {
        int left = 0;
        int right = arr.length-1;
        while(left < right) {
            while(left < right && arr[left] % 2 == 0) {
                left++;
            }
            while(left < right && arr[right] % 2 != 0) {
                right--;
            }
            int tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;
        }
    }
    public static void main235466(String[] args) {
        int[] arr = {1,2,3,4,5,6,7};
        trans(arr);
        System.out.println(Arrays.toString(arr));

    }
}
